package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IHWCTbdepartmentDAO;
import com.trkj.dao.jpa.IHwcPostDAO;
import com.trkj.dao.jpa.IHwcPowergroupDAO;
import com.trkj.dao.jpa.IHwcRoleDAO;
import com.trkj.dao.jpa.IHwcStaffDAO;
import com.trkj.dao.jpa.IHwcStaffPowerDAO;
import com.trkj.dao.jpa.IHwcTitleDAO;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbpost;
import com.trkj.pojos.Tbrole;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.Tbstaffpower;
import com.trkj.pojos.Tbtitle;
import com.trkj.vo.HwcStaffVO;

@Service
@Transactional
public class HwcStaffService {
	
	@Autowired
	private IHwcStaffDAO staffDAO;
	
	@Autowired
	private  IHWCTbdepartmentDAO deptDAO;
	
	@Autowired
	private IHwcRoleDAO roleDAO;
	
	@Autowired
	private IHwcTitleDAO titleDAO;
	
	@Autowired
	private IHwcPostDAO	postDAO;
	
	@Autowired
	private	IHwcPowergroupDAO	pgDAO;	//角色权限表
	
	@Autowired
	private IHwcStaffPowerDAO	spDAO;	//员工权限表
	
	
	/**
	 * 根据员工账号 查询该员工科室的其他员工
	 * @param staffAccount
	 * @return
	 */
	public List<Tbstaff> findAccountByStaff(){
		
		return staffDAO.findAccountByStaff();
		
	}
	/**
	 * 根据部门编号查询员工集合
	 * @param departmentNo
	 * @return
	 */
	public List<Tbstaff> findDeptNoByStaffs(long departmentNo){
		return staffDAO.findDeptNoByStaff(new BigDecimal(departmentNo));
	}
	
	/**
	 * 分页显示员工信息--包含离职
	 */
	public Map<String,Object> queryStaffPage(int page,int rows){
	PageRequest pr = new PageRequest(page -1,rows);
	//获取当前页记录
	List<Tbstaff> list = staffDAO.findTbstaffPage(pr);
	//获取总的记录条数 
	long total  = staffDAO.count();
	
	Map<String,Object> map = new HashMap<>();
	map.put("total", total);
	map.put("rows", poStaffListToVOList(list));
	return map;	
	}
	/**
	 * 分页显示员工信息--不包含离职
	 */
	public Map<String,Object> queryStaffPageNoOut(int page,int rows){
	PageRequest pr = new PageRequest(page -1,rows);
	//获取当前页记录
	List<Tbstaff> list = staffDAO.findTbstaffPageNoOut(pr);
	//获取总的记录条数 
	long total  = staffDAO.count();
	Map<String,Object> map = new HashMap<>();
	map.put("total", total);
	map.put("rows", poStaffListToVOList(list));
	return map;	
	}
	/**
	 * 根据姓名分页显示员工信息（模糊查询）不包含离职 
	 * @param page
	 * @param rows
	 * @param staffName
	 * @return
	 */
	public Map<String,Object> findStaffNameNoOut(int page,int rows,String staffName){
		PageRequest pr = new PageRequest(page -1,rows);
		//获取当前页记录
		List<Tbstaff>	list= staffDAO.findStaffNameByPageNoOut(pr, staffName);
		//获取总的记录条数 
		long total  = staffDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", poStaffListToVOList(list));
		return map;	
	}
	/**
	 * 根据科室分页显示员工信息（模糊查询）不包含离职
	 * @param page
	 * @param rows
	 * @param departmentName
	 * @return
	 */
	public Map<String,Object> findDeptNameNoOut(int page,int rows,String departmentName){
		PageRequest pr = new PageRequest(page -1,rows);
		//获取当前页记录
		List<Tbstaff>	list =	staffDAO.findStaffDeptByPageNoOut(pr, departmentName);
		//获取总的记录数
		long total  = staffDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", poStaffListToVOList(list));
		return map;	
	}
	/**
	 * 查找所有科室
	 */
	public List<Tbdepartment> queryAllDepartment(){
		return deptDAO.queryAllTbdepartment();
	}
	/**
	 * 查找所有职务
	 * @return
	 */
	public List<Tbpost> queryAllPost(){
		return  postDAO.queryAllTbpost();
	}
	/**
	 *  查找所有职称
	 */
	public List<Tbtitle>qeuryAllTitle(){
		return titleDAO.queryAllTbtitle();
	}
	/**
	 * 查询所有角色
	 */
	public List<Tbrole> queryAllRole(){
		return roleDAO.queryAllTbRole();
	}
	
	/**
	 * 添加员工
	 */
	public	void addNewStaff(Tbstaff staff){		
		//保存员工账号
		staffDAO.save(staff);
	//	saveRolePower(staff.getStaffNo(),staff.getStaffRole().longValue());
		saveRolePower(staff.getStaffNo(),staff.getStaffRole());
		//调取生成员工账号方法
		createAccount(staff.getStaffNo());
		
	}
	/**
	 * 给添加员工添加权限集合
	 * @param roleNo
	 */
	private void saveRolePower(long staffNo,BigDecimal roleNo ){
		
		List<BigDecimal> list = pgDAO.queryRoleByPower(roleNo);
		
		for(BigDecimal powerNo : list){
			Tbstaffpower sp = new Tbstaffpower();
			sp.setPowersNo(powerNo);
			sp.setStaffNo(new BigDecimal(staffNo));
			spDAO.save(sp);
		}
	}
	/**
	 * 修改员工信息
	 */
	public	void editStaff(Tbstaff staff){
		
		Tbstaff tbs=staffDAO.findOne(staff.getStaffNo());
		tbs.setStaffSex(staff.getStaffSex());
		tbs.setStaffAge(staff.getStaffAge());
		tbs.setStaffIdcard(staff.getStaffIdcard());
		tbs.setStaffName(staff.getStaffName());
		tbs.setStaffPhone(staff.getStaffPhone());
		tbs.setStaffPostNo(staff.getStaffPostNo());
		tbs.setStaffRole(staff.getStaffRole());
		tbs.setStaffTitle(staff.getStaffTitle());
		tbs.setDepartmentNo(staff.getDepartmentNo());
		staffDAO.save(tbs);
		
	}
	/**
	 * 重置员工密码
	 * @param staffNos
	 */
	public void resetStaffpwd(Long[] staffNos){
		//将数组转换成集合
		DefaultConversionService service = 
				new DefaultConversionService();
		List<Long> list = service.convert(staffNos, List.class);
		for(Long l : list){
			Tbstaff staff=staffDAO.findOne(l);
				staff.setStaffPassword("000000");
				staffDAO.save(staff);
		}
		
		
	}
	/**
	 * 删除员工
	 */
	public void deleteTbStaff(Long[] staffNos){
		//将数组转换成集合
		DefaultConversionService service = 
				new DefaultConversionService();
		List<Long> list = service.convert(staffNos, List.class);
		
		for(Long l : list){
			staffDAO.delete(l);
		}
	}
	/**
	 * 员工离职
	 * @param staffNos
	 */
	public void quitStaff(Long[] staffNos){
		//将数组转换成集合
		DefaultConversionService service = 
						new DefaultConversionService();
		List<Long> list = service.convert(staffNos, List.class);
		for(Long l:list){
			Tbstaff staff = staffDAO.findOne(l);
			staff.setStaffState("离职");
			staffDAO.save(staff);
			//System.out.println("--------------------------------------------------------------------\n"+staff.getStaffName()+staff.getStaffState());
		}
	}
	/**
	 * 根据员工编号查找员工
	 */
	public Tbstaff findStaffById(long staffno){
		
		Tbstaff staff = staffDAO.findOne(staffno);
		Tbstaff temp = new Tbstaff();
		copyStaff(staff,temp);
		return temp;
	}
	/**
	 * 属性赋值
	 */
	private void copyStaff(Tbstaff src,Tbstaff desc){
		
		desc.setStaffNo(src.getStaffNo());
		desc.setStaffName(src.getStaffName());
		desc.setStaffSex(src.getStaffSex());
		desc.setStaffPhone(src.getStaffPhone());
		desc.setStaffAccount(src.getStaffAccount());
		desc.setStaffAge(src.getStaffAge());
		desc.setStaffIdcard(src.getStaffIdcard());
		desc.setStaffPassword(src.getStaffPassword());
		desc.setStaffState(src.getStaffState());
		
		desc.setDepartmentNo(src.getDepartmentNo());
		desc.setStaffPostNo(src.getStaffPostNo());
		desc.setStaffRole(src.getStaffRole());
		desc.setStaffTitle(src.getStaffTitle());
	}
	/**
	 * POJO集合转换成VO集合
	 * @param list
	 * @return
	 */
	
	private	List<HwcStaffVO> poStaffListToVOList(List<Tbstaff> list){
		if(list == null) return null;
		List<HwcStaffVO> listVO = new ArrayList<>();
		for(Tbstaff staff : list){
			HwcStaffVO staffVO = staffTOVO(staff);
			listVO.add(staffVO);
		}
		return listVO;
	}
	/**
	 * 查询职业专有职称
	 */
	public List<Tbtitle> queryAllTitlePostno(long postno){
		return titleDAO.queryAllTbtitlePostno(new BigDecimal(postno));
	}
	
	/**
	 * 自动生成员工账号和密码
	 */
	public void  createAccount(long staffno){
		System.out.print(staffno);;
		String account="st";
		//获取年份
		int year = new java.util.Date().getYear() + 1900;
		//组合
		account = account + year + staffno;
		
		Tbstaff staff = staffDAO.findOne(staffno);
		staff.setStaffAccount(account);
		
		System.err.println("员工账号:"+account);
		staff.setStaffPassword("000000");
		staff.setStaffState("在职");
		staffDAO.save(staff);
	}
	

	private	HwcStaffVO	staffTOVO(Tbstaff staff){
		
		
		Tbpost post = postDAO.findOne(staff.getStaffPostNo().longValue());
		Tbtitle title = titleDAO.findOne(staff.getStaffTitle().longValue());
		
	
		Tbrole	role = roleDAO.findOne(staff.getStaffRole().longValue());
	
		Tbdepartment	dept = deptDAO.findOne(staff.getDepartmentNo().longValue());
		
		HwcStaffVO staffVO  = new HwcStaffVO();
	
		
		staffVO.setStaffNo(staff.getStaffNo());
		staffVO.setStaffName(staff.getStaffName());
		staffVO.setStaffAccount(staff.getStaffAccount());
		staffVO.setStaffAge(staff.getStaffAge());
		staffVO.setStaffIdcard(staff.getStaffIdcard());
		staffVO.setStaffPassword(staff.getStaffPassword());
		staffVO.setStaffPhone(staff.getStaffPhone());
		staffVO.setStaffSex(staff.getStaffSex());
		staffVO.setStaffState(staff.getStaffState());
		
		
		
		staffVO.setStaffPostNo(staff.getStaffPostNo());		
		staffVO.setStaffRole(staff.getStaffRole());
		staffVO.setStaffTitle(staff.getStaffTitle());
		staffVO.setDepartmentNo(staff.getDepartmentNo());
		
		staffVO.setRoleName(role == null ? null : role.getRoleName());		
		staffVO.setPostName(post == null ? null : post.getPostName());
		staffVO.setTitleName(title == null ?null : title.getTitleName());
		staffVO.setDepartmentName(dept == null ? null :  dept.getDepartmentName());
		
		return staffVO;
	}
	/**
	 * 通过名称分页显示员工（模糊查询）
	 * @param page
	 * @param rows
	 * @param staffName
	 * @return	map
	 */
	public Map<String,Object>findStaffNameByPage(int page,int rows,String staffName){
		PageRequest pagerble  = new PageRequest(page -1,rows);
		//获取当前页记录
		List<Tbstaff> list = staffDAO.findStaffNameByPage(pagerble, staffName);
		//获取总的记录数
		long total = staffDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", poStaffListToVOList(list));
		//System.err.println(" 分页显示员工名称（模糊查询）Service"+map.get("rows"));
		return map;
		
	}
	/**
	 * 通过部门名称模糊查询该部门的员工
	 * @param page
	 * @param rows
	 * @param departmentName
	 * @return
	 */
	public Map<String,Object>findDeptNameByPage(int page,int rows,String departmentName){
		PageRequest pagerble = new PageRequest(page -1,rows);
		//获取当前页记录
		List<Tbstaff> list = staffDAO.findDeptNameByPage(pagerble,departmentName);
		//获取总的记录数
		long	total = staffDAO.count();
		Map<String,Object> map = new HashMap<>();
		map.put("total", list.size());
		map.put("rows", poStaffListToVOList(list));
		
		return map;
	}
	
	/**
	 * 登录验证
	 */
	
	public  boolean findAccountByStaff(String staffAccount,String password){
		
		//long staffNo = 0l;
		//获取账号7位以后转换为Long类型
		//获取ID
		long staffNo =Long.parseLong(staffAccount.substring(6));
		//staffNo =staffAccountTOStaffNo(staffAccount);
		//System.out.println("获取账号"+staffAccount);
		//System.out.println("获取的编号"+staffNo);
		
		//查询该用户
		Tbstaff staff = staffDAO.findOne(staffNo);
		if(staff == null){
			return false;
		}else if(!(staff.getStaffPassword().equals(password))){
			return false;
		}
		return true;
		
	}
	/**
	 * 将员工账号转化成员工编号
	 * @param account
	 * @return
	 */
	/*public long staffAccountTOStaffNo(String staffAccount){
		long staffNo =Long.parseLong(staffAccount.substring(6));
		return staffNo;
	}*/
	
	/**
	 * 根据员工账号查询员工名称
	 */
	public String findAccountBySName(String staffAccount){
		long staffNo =Long.parseLong(staffAccount.substring(6));
		Tbstaff staff = staffDAO.findOne(staffNo);
		if(staff == null){
			return "";
		}
		return staff.getStaffName();
		
	}
	
}




